package libraries;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.hssf.util.HSSFColor;
public class Excelwrite {
	
	public static String xlwrite(String xlPath, String xlName, String[][] xldata) throws Exception {
		
	try {
		
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yMd_hhmmss");
		String formattedDate = sdf.format(date);
		String xlTC_res_temp = xlPath + xlName+"_"+formattedDate+".xls";
		//String xlTC_res_temp2 = xlPath + xlName+"_"+"StyleDemo"+".xls";
		
    	File outFile = new File(xlTC_res_temp);    	
        HSSFWorkbook wb = new HSSFWorkbook();
        
        System.out.println("Writing Excel file..");
        HSSFSheet osheet = wb.createSheet("TestCase_Res");
        
    	// Style the cell with borders all around.
	    CellStyle style = wb.createCellStyle();
	    style.setBorderBottom(CellStyle.BORDER_THIN);
	    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
	    style.setBorderLeft(CellStyle.BORDER_THIN);
	    style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
	    style.setBorderRight(CellStyle.BORDER_THIN);
	    style.setRightBorderColor(IndexedColors.BLACK.getIndex());
	    style.setBorderTop(CellStyle.BORDER_THIN);
	    style.setTopBorderColor(IndexedColors.BLACK.getIndex());
	    
       // Header Formatting
        HSSFCellStyle headerstyle = wb.createCellStyle();
        headerstyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
 	    headerstyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
 	    HSSFFont headerfont = wb.createFont();
 	    headerfont.setBoldweight(Font.BOLDWEIGHT_BOLD);
 	    headerstyle.setFont(headerfont);
 	    headerstyle.setBorderBottom(CellStyle.BORDER_THIN);
 	    headerstyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
 	    headerstyle.setBorderLeft(CellStyle.BORDER_THIN);
 	    headerstyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
 	    headerstyle.setBorderRight(CellStyle.BORDER_THIN);
 	    headerstyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
 	    headerstyle.setBorderTop(CellStyle.BORDER_THIN);
 	    headerstyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
 	    
 	  //Test Result = Pass Formatting  
 	    HSSFCellStyle res_pass_style = wb.createCellStyle();
 	    res_pass_style.setFillForegroundColor(HSSFColor.GREEN.index);
 	    res_pass_style.setFillPattern(CellStyle.SOLID_FOREGROUND);
 	    HSSFFont res_font = wb.createFont();
 	    res_font.setColor(HSSFColor.WHITE.index);
 	    res_pass_style.setFont(res_font);
 	    res_pass_style.setBorderBottom(CellStyle.BORDER_THIN);
 	    res_pass_style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
 	    res_pass_style.setBorderLeft(CellStyle.BORDER_THIN);
 	    res_pass_style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
 	    res_pass_style.setBorderRight(CellStyle.BORDER_THIN);
 	    res_pass_style.setRightBorderColor(IndexedColors.BLACK.getIndex());
 	    res_pass_style.setBorderTop(CellStyle.BORDER_THIN);
 	    res_pass_style.setTopBorderColor(IndexedColors.BLACK.getIndex());
 	    
 	 //Test Result = Fail Formatting  
 	    HSSFCellStyle res_fail_style = wb.createCellStyle();
 	    res_fail_style.setFillForegroundColor(HSSFColor.RED.index);
 	    res_fail_style.setFillPattern(CellStyle.SOLID_FOREGROUND);
 	    HSSFFont res_fail_font = wb.createFont();
 	    res_fail_font.setColor(HSSFColor.WHITE.index);
 	    res_fail_style.setFont(res_fail_font); 
 	    res_fail_style.setBorderBottom(CellStyle.BORDER_THIN);
 	    res_fail_style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
 	    res_fail_style.setBorderLeft(CellStyle.BORDER_THIN);
 	    res_fail_style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
 	    res_fail_style.setBorderRight(CellStyle.BORDER_THIN);
 	    res_fail_style.setRightBorderColor(IndexedColors.BLACK.getIndex());
 	    res_fail_style.setBorderTop(CellStyle.BORDER_THIN);
 	    res_fail_style.setTopBorderColor(IndexedColors.BLACK.getIndex());
 	    
 	   //Test Result = Skipped Formatting  
 	    HSSFCellStyle res_skip_style = wb.createCellStyle();
 	   res_skip_style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
 	   res_skip_style.setFillPattern(CellStyle.SOLID_FOREGROUND);
 	   HSSFFont res_skip_font = wb.createFont();
 	   res_skip_font.setColor(HSSFColor.BLACK.index);
 	   res_skip_style.setFont(res_skip_font);
 	   res_skip_style.setBorderBottom(CellStyle.BORDER_THIN);
 	   res_skip_style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
 	   res_skip_style.setBorderLeft(CellStyle.BORDER_THIN);
 	   res_skip_style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
 	   res_skip_style.setBorderRight(CellStyle.BORDER_THIN);
 	   res_skip_style.setRightBorderColor(IndexedColors.BLACK.getIndex());
 	   res_skip_style.setBorderTop(CellStyle.BORDER_THIN);
 	   res_skip_style.setTopBorderColor(IndexedColors.BLACK.getIndex());
 	    
    	  for (int myrow = 0; myrow < xldata.length; myrow++) {
            HSSFRow row = osheet.createRow(myrow);
	         for (int mycol = 0; mycol < xldata[1].length; mycol++) {
	        	HSSFCell cell = row.createCell(mycol);
	        	  
	        	cell.setCellType(Cell.CELL_TYPE_STRING);
	        	cell.setCellValue(xldata[myrow][mycol]);
	        	cell.setCellStyle(style);  
	        	    if (myrow == 0) {
	        	    cell.setCellStyle(headerstyle);
	        	      }
	        	    if (myrow > 0 && (mycol==2 || mycol==3) && xldata[myrow][mycol].equals("Pass")) {
		        	    cell.setCellStyle(res_pass_style);
		        	      }
	        	    if (myrow > 0 && (mycol==2 || mycol==3) && xldata[myrow][mycol].equals("Fail")) {
		        	    cell.setCellStyle(res_fail_style);
		        	      }
	        	    if (myrow > 0 && (mycol==2 || mycol==3) && xldata[myrow][mycol].equals("Skipped") ) {
		        	    cell.setCellStyle(res_skip_style);
		        	      }
	             } 
	        osheet.autoSizeColumn((short)0); 
	        osheet.autoSizeColumn((short)1);
	        osheet.autoSizeColumn((short)2);
	        osheet.autoSizeColumn((short)3);
	        FileOutputStream fOut = new FileOutputStream(outFile);
	        wb.write(fOut);
	        fOut.flush();
	        fOut.close();
	        
    	  }
    	  return "Pass";
    }
	catch (Exception e) {
		// TODO Auto-generated catch block
		System.out.println(e.getMessage());
		return "Fail" + e.getMessage();
		
	}
	}	
	
}



